package com.thinkgem.jeesite.modules.oa.service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.modules.oa.dao.OaFileNumDao;
import com.thinkgem.jeesite.modules.oa.entity.Element;
import com.thinkgem.jeesite.modules.oa.entity.Firm;
import com.thinkgem.jeesite.modules.oa.entity.OaFile;
import com.thinkgem.jeesite.modules.oa.entity.SerialNum;

@Service
public class OaFileNumService extends CrudService<OaFileNumDao, OaFile>{
	@Autowired
	private OaFileNumDao oaFileNumDao;

	// 找到最后一条数据
	public OaFile findLastOaFile() {

		return oaFileNumDao.findLastOaFile();
	}

	// 保存数据
	@Transactional(readOnly = false)
	public void savaOaFile(OaFile oaFile) {

		// 设置创建时间以及更新时间
		Date createDate = new Date();
		SimpleDateFormat bartDateFormat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		String years = createDate.toString();
		years = years.substring(years.length() - 4, years.length());
		oaFile.setCreateDate(createDate);
		oaFile.setUpdateDate(createDate);
		String createTime = bartDateFormat.format(createDate);
		oaFile.setCreateTime(createTime);
		// 设定最终编号(最终编号=部门代码+文件代码+年份+流水号：流水号为6位，不足六位补0)
		String code = "0";
		for (int i = 0; i < (5 - (oaFile.getNum() + "").length()); i++) {
			code = code + "0";
		}
		String oaFileCode = oaFile.getDeptCode() + oaFile.getFileType() + years
				+ code + oaFile.getNum();
		oaFile.setOaFileCode(oaFileCode);
		oaFileNumDao.savaOaFile(oaFile);
	}
	@Transactional(readOnly = false)
	public List<Element> findElementNames() {
		
		List<Element> list= oaFileNumDao.findElementNames();
		
		return list;
	}
	@Transactional(readOnly = false)
	public List<Firm> findFirmNames() {
		List<Firm> list= oaFileNumDao.findFirmNames();
		return list;
	}
	@Transactional(readOnly = false)
	public SerialNum findLastSerialNum(String firmCode) {
		// TODO Auto-generated method stub
		return oaFileNumDao.findLastSerialNum(firmCode);
	}
	@Transactional(readOnly = false)
	public void savaSerialNum(SerialNum serialNum) {
		// 设置创建时间以及更新时间
				Date createDate = new Date();
				serialNum.setCreateDate(createDate);
				serialNum.setUpdateDate(createDate);
		// 设定最终编号
				String code = "0";
				for (int i = 0; i < (5 - (serialNum.getSerialCode() + "").length()); i++) {
					code = code + "0";
				}
				String finalNum=serialNum.getFirmCode()+"MTSH005"+code+serialNum.getElementCode()+"-"+serialNum.getSerialCode();
				serialNum.setFinalNum(finalNum);
				oaFileNumDao.savaSerialNum(serialNum);
			}
}
